package com.zl.tby.service.impl;

import com.zl.tby.core.base.bean.OptionType;
import com.zl.tby.core.base.bean.TbyPageResult;
import com.zl.tby.core.base.util.ToolsUtil;
import com.zl.tby.core.common.R;
import com.zl.tby.dto.TbyWorkDynamicsQuery;
import com.zl.tby.model.TbyWorkDynamics;
import com.zl.tby.repository.TbyWorkDynamicsRepo;
import com.zl.tby.service.ITbyWorkDynamicsService;
import com.zl.tby.vo.TbyWorkDynamicsForm;
import org.noear.solon.annotation.Component;
import org.noear.solon.data.annotation.Tran;
import org.noear.wood.IPage;
import org.noear.wood.annotation.Db;
import org.noear.wood.utils.StringUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 工作动态接口实现类
 * @author sunjiaxing
 * @version 1.0
 * @since 2024/8/22 00:23
 */
@Component
public class TbyWorkDynamicsServiceImpl implements ITbyWorkDynamicsService {

    @Db
    private TbyWorkDynamicsRepo tbyWorkDynamicsRepo;

    @Override
    @Tran
    public R<TbyWorkDynamics> add(TbyWorkDynamicsForm form) {
        TbyWorkDynamics entity = form.createEntity();
        entity.setCreateBy(ToolsUtil.getLoginUserId());
        if (entity.getCreateTime() == null) {
            entity.setCreateTime(LocalDateTime.now());
        }
        long id = tbyWorkDynamicsRepo.insert(entity, true);
        entity.setId(id);
        return R.ok(entity);
    }

    @Override
    @Tran
    public R<TbyWorkDynamics> update(TbyWorkDynamicsForm form) {
        TbyWorkDynamics entity = this.get(form.getId());
        if (entity == null) {
            return R.failed("数据不存在");
        }
        form.updateEntity(entity);
        entity.setUpdateBy(ToolsUtil.getLoginUserId());
        entity.setUpdateTime(LocalDateTime.now());
        tbyWorkDynamicsRepo.updateById(entity, false);
        return R.ok(entity);
    }

    @Override
    public R<Void> delete(Long id) {
        int flag = tbyWorkDynamicsRepo.deleteById(id);
        return flag > 0 ? R.ok() : R.failed("删除失败");
    }

    @Override
    public TbyWorkDynamics get(Long id) {
        return tbyWorkDynamicsRepo.selectById(id);
    }

    @Override
    public TbyPageResult<TbyWorkDynamics> page(TbyWorkDynamicsQuery query) {
        IPage<TbyWorkDynamics> iPage = tbyWorkDynamicsRepo.selectPage(query.getOffset(), query.getSize(), mp -> {
            mp.whereTrue()
                    .andIf(StringUtils.isNotEmpty(query.getTitle()), "title like ?", "%" + query.getTitle() + "%")
                    .andIf(ToolsUtil.isNotNull(query.getType()), "type = ?", query.getType())
                    .andIf(StringUtils.isNotEmpty(query.getCreateTimeBegin()), " create_time >= ?", query.getCreateTimeBegin() + " 00:00:00")
                    .andIf(StringUtils.isNotEmpty(query.getCreateTimeEnd()), " create_time <= ?", query.getCreateTimeEnd() + " 23:59:59");
            if (StringUtils.isNotEmpty(query.getOrder())) {
                mp.orderBy(query.getOrder());
            }
        });
        iPage.getList().forEach(item -> {
            item.setCreateTimeFormat(item.getCreateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        });
        return new TbyPageResult(iPage, query);
    }

    @Override
    public List<TbyWorkDynamics> list(TbyWorkDynamicsQuery query) {
        return tbyWorkDynamicsRepo.selectList(mp -> {
            mp.whereTrue()
                    .andIf(StringUtils.isNotEmpty(query.getTitle()), "title like ?", "%" + query.getTitle() + "%")
                    .andIf(ToolsUtil.isNotNull(query.getType()), "type = ?", query.getType())
                    .andIf(StringUtils.isNotEmpty(query.getCreateTimeBegin()), " create_time >= ?", query.getCreateTimeBegin() + " 00:00:00")
                    .andIf(StringUtils.isNotEmpty(query.getCreateTimeEnd()), " create_time <= ?", query.getCreateTimeEnd() + " 23:59:59");
            if (StringUtils.isNotEmpty(query.getOrder())) {
                mp.orderBy(query.getOrder());
            }
        });
    }
}
